<?php
// Create tree
function generateTree($data,$pid=0){
    $arr=[];
    $tree=[];
    foreach($data as $v){
        $arr[$v['id']]=$v;
    }
    foreach($arr as $k=>$v){
        if($v['pid']==$pid){
            $tree[]=&$arr[$k];
        }else{
            $arr[$v['pid']]['children'][]=&$arr[$k];
        }
    }
    return $tree;
}
//取得随机数
function makeStr($length)
{
    $chars = array(
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
        'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
        't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
        'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
        'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '!',
        '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_',
        '[', ']', '{', '}', '<', '>', '~', '`', '+', '=', ',',
        '.', ';', ':', '/', '?', '|'
    );
    $keys = array_rand($chars, $length);
    $str = '';
    for ($i = 0; $i < $length; $i++) {
        $str .= $chars[$keys[$i]];
    }
    return $str;
}


function layoutData($dataList, $key = 0)
{
    $tree = [];
    foreach ($dataList as $k => $v) {
        if ($v['pid'] == $key) {
            $children = layoutData($dataList, $v['id']);
            if ($children) {
                $v['children'] = $children;
            }
            $v['meta'] = $v;
            $tree[] = $v;
        }
    }
    return $tree;
}

/**
 * 本年所有月份 开始和结束
 * @return array
 */
function getMonth(){
    $year = date('Y');
    $yeararr = [];
    $month = [];
    for ($i=1; $i <=12 ; $i++) {
        $yeararr[$i] = $year.'-'.$i;
    }
    foreach ($yeararr as $key => $value) {
        $timestamp = strtotime( $value );
        $start_time = date( 'Y-m-1 00:00:00', $timestamp );
        $mdays = date( 't', $timestamp );
        $end_time = date( 'Y-m-' . $mdays . ' 23:59:59', $timestamp );

        $month[$key]['start_time'] = strtotime($start_time);
        $month[$key]['end_time'] = strtotime($end_time);

    }
    return $month;
}

/**
 * 获取折线图数据
 * @param $data
 * @return void
 */
function getBrokenLineChart($data)
{
    $series_data = [];
    foreach ($data['value_data'] as $value_data){
        $series_data[] = [
            'name'=>$value_data['name'],
            'type'=>'line',
            'stack'=>'Total',
            'data'=>$value_data['value'],
        ];
    }
    $option = [
        'title'=>[
          'text'=>$data['title']
        ],
        'tooltip'=>[
            'trigger'=>'axis'
        ],
        'legend'=>[
            'data'=>$data['line_name']
        ],
        'grid'=>[
            'left'=>'3%',
            'right'=>'4%',
            'bottom'=>'3%',
            'containLabel'=>true,
        ],
        'toolbox'=>[
            'feature'=>['saveAsImage'=>(object)[]],
        ],
        'xAxis'=>[
            'type'=>'category',
            'boundaryGap'=>false,
            'data'=>$data['month_data'],
        ],
        'yAxis'=>[
            'type'=>'value',
        ],
        'series'=>$series_data
    ];
    return (object)$option;
}

/**
 * 获取文件类型
 * @param $filename
 * @return string
 */
function getFileType($filename)
{
    $file = fopen($filename, "rb");
    $bin = fread($file, 2); //只读2字节
    fclose($file);
    $strInfo = @unpack("C2chars", $bin); //将字节转化为字节二进制的数组](https://segmentfault.com/a/1190000020073071)
    $typeCode = intval($strInfo['chars1'].$strInfo['chars2']); //intval函数作用是提取整数(参数包括字符串和整数)
    $fileType = '';
    switch ($typeCode)
    {
        case 208207:
            $fileType = 'word';//xls.doc.ppt
            break;
        case 3780:
            $fileType = 'word';//pdf
            break;
//        case 6063:
//            $fileType = 'php';
//            break;
//        case 7790:
//            $fileType = 'exe';
//            break;
//        case 7784:
//            $fileType = 'midi';
//            break;
        case 8297:
            $fileType = 'attachment';//rar
            break;
        case 8075:
            //获取文件后缀
            $fileType = 'attachment';//xlsx,zip,pptx,mmap
            $exe =  substr(strrchr($filename,'.'),1);
            if($exe != 'zip'){
                $fileType = 'word';//xlsx,zip,pptx,mmap
            }
            break;
        case 7173:
            $fileType = 'image';//gif
            break;
        case 255216:
            $fileType = 'image';//jpg
            break;
        case 7173:
            $fileType = 'image';//gif
            break;
        case 6677:
            $fileType = 'image';//bmp
            break;
        case 13780:
            $fileType = 'image';//png
            break;
        default:
            $fileType = '';
    }

    //Fix
    if ($strInfo['chars1']=='-1' AND $strInfo['chars2']=='-40' ) return 'image';//jpg
    if ($strInfo['chars1']=='-119' AND $strInfo['chars2']=='80' ) return 'image';//png

    return $fileType;
}